Page({
  data: {
    isPlaying: false,
    currentSong: null,
    playlists: [],
    banners: [],
    hotSongs: []
  },
  
  onLoad() {
    this.initData()
    this.setupAudioListeners()
  },
  

  initData() {
    const app = getApp()
    
    // 初始化歌单数据
    const playlists = [
      {
        id: 1,
        name: '热门推荐',
        coverImgUrl: '/assets/playlist-classic.svg',
        count: 8
      },
      {
        id: 2,
        name: '新歌首发',
        coverImgUrl: '/assets/playlist-hot.svg',
        count: 7
      },
      {
        id: 3, 
        name: '经典老歌',
        coverImgUrl: '/assets/playlist-kpop.svg',
        count: 8
      },
      {
        id: 4,
        name: '欧美流行',
        coverImgUrl: '/assets/playlist-pop.svg',
        count: 7
      },
      {
        id: 5,
        name: 'K-POP精选',
        coverImgUrl: '/assets/playlist-new.svg',
        count: 7
      },
      {
        id: 6,
        name: '影视原声',
        coverImgUrl: '/assets/playlist-movie.svg',
        count: 7
      }
    ]

    // 初始化轮播图数据
    const banners = [
      {
        id: 1,
        image: '/assets/banner-festival.svg',
        title: '国庆特别推荐'
      },
      {
        id: 2,
        image: '/assets/banner-newmusic.svg',
        title: '新歌速递'
      }
    ]

    // 获取热门歌曲（从第一个歌单）
    const config = require('../../utils/config')
    const hotSongs = config.getPlaylistSongs(1).slice(0, 6) // 取前6首作为热门歌曲
    
    this.setData({
      playlists: playlists,
      banners: banners,
      hotSongs: hotSongs,
      currentSong: app.globalData.currentSong,
      isPlaying: app.globalData.isPlaying
    })
  },

  setupAudioListeners() {
    const app = getApp()
    
    if (app.globalData.audioManager) {
      // 监听播放状态变化
      app.globalData.audioManager.onPlay(() => {
        this.setData({ isPlaying: true })
      })
      
      app.globalData.audioManager.onPause(() => {
        this.setData({ isPlaying: false })
      })
    }
  },
  
  onShow() {
    // 页面显示时更新当前歌曲信息
    const app = getApp()
    this.setData({
      currentSong: app.globalData.currentSong,
      isPlaying: app.globalData.isPlaying
    })
    // 更新收藏状态
    this.updateFavoriteStatus()
  },

  // 更新收藏状态
  updateFavoriteStatus() {
    const app = getApp()
    const currentUser = app.globalData.userInfo
    if (!currentUser) {
      // 未登录时重置收藏状态
      const hotSongs = this.data.hotSongs.map(song => ({
        ...song,
        isFavorited: false
      }))
      this.setData({ hotSongs })
      return
    }

    // 更新热门歌曲的收藏状态
    const hotSongs = this.data.hotSongs.map(song => ({
      ...song,
      isFavorited: app.isFavorite(song.id)
    }))
    
    this.setData({ hotSongs })
  },

  // 切换收藏状态
  toggleFavorite(e) {
    e.stopPropagation()
    const app = getApp()
    const songId = e.currentTarget.dataset.songid
    const song = this.data.hotSongs.find(s => s.id === songId)
    
    if (!song) return

    // 使用app中的统一收藏方法
    const result = app.toggleFavorite(song)
    
    // 更新收藏状态
    this.updateFavoriteStatus()
  },

  // 播放歌曲
  playSong(e) {
    e.stopPropagation()
    const app = getApp()
    const songId = e.currentTarget.dataset.songid
    const song = app.globalData.songs.find(s => s.id === songId)
    
    if (song) {
      app.playSong(song)
      wx.navigateTo({
        url: `/pages/player/player?musicId=${song.id}&name=${encodeURIComponent(song.name)}&artist=${encodeURIComponent(song.artist)}&cover=${encodeURIComponent(song.picUrl || '/assets/default-cover.svg')}&duration=${encodeURIComponent(song.duration || '00:00')}`
      })
    }
  },
  
  togglePlay(e) {
    e.stopPropagation()
    const app = getApp()
    if (app.globalData.isPlaying) {
      app.globalData.audioManager.pause()
    } else {
      if (app.globalData.currentSong) {
        app.playSong(app.globalData.currentSong)
      }
    }
  },
  
  goPlaylist(e) {
    const playlistId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/playlist/playlist?id=${playlistId}`
    })
  },
  
  goPlayer() {
    if (!this.data.currentSong) {
      wx.showToast({
        title: '暂无播放歌曲',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/player/player'
    })
  }
})